package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.DishFlavorMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;


/**
 * @author ljk74
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    private void cleanCache(){
        Set keys = redisTemplate.keys(RedisConstant.DISH_CACHE_PREFIX + '*');
        redisTemplate.delete(keys);
    }
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.insertDish(dish);
        //获取添加后的id
        Long id = dish.getId();
        List<DishFlavor> list = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(list)){
            for (DishFlavor flavor : list) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.insertDishFlavor(list);
        }
        cleanCache();
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Dish dish = Dish.builder()
                .name(dishPageQueryDTO.getName())
                .categoryId(dishPageQueryDTO.getCategoryId())
                .status(dishPageQueryDTO.getStatus())
                .build();
        //发现前端的分类名一直显示不出来，发现需要传出分类名字，所以改为DishVo
        //List<Dish> list = dishMapper.pageQuery(dish);
        List<DishVO> list = dishMapper.pageQuery(dish);

        Page<DishVO> p = (Page<DishVO>) list;
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    @Transactional //声明式事务
    public void delDishes(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return;
        }
        //先查
        List<Dish> list = dishMapper.selectByIds(ids);
        if (!CollectionUtils.isEmpty(list)){
            throw new BaseException(MessageConstant.DISH_ON_SALE);
        }
        //TODO 套餐中包含的菜品不能删
        dishFlavorMapper.delByDishIds(ids);
        dishMapper.delByIds(ids);
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        List<Dish> list = dishMapper.selectByCategoryId(categoryId);
        if (CollectionUtils.isEmpty(list)){
            throw new BaseException(MessageConstant.DISH_NOT_FOUND);
        }
        return list;
    }

    @Override
    @Transactional
    public DishVO selectDishWithFlavor(Long dishId) {
        Dish dish = dishMapper.selectById(dishId);
        if (dish == null){
            throw new BaseException(MessageConstant.DISH_NOT_FOUND);
        }
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(dishId);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        //1.修改菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //2.删除口味表
        Long dishId = dishDTO.getId();
        Dish dish1 = dishMapper.selectById(dishId);
        if (dish1==null){
            throw new BaseException(MessageConstant.DISH_NOT_FOUND);
        }
        dishFlavorMapper.delByDishIds(List.of(dishId));
        //3.新增口味表
        List<DishFlavor> list = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(list)){
            for (DishFlavor flavor : list) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insertDishFlavor(list);
        }
        cleanCache();
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
        cleanCache();
    }
}
